package com.example.wxai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.wxai.mapper.CommentMapper;
import com.example.wxai.mapper.EssayMapper;
import com.example.wxai.mapper.LikesMapper;
import com.example.wxai.mapper.UserMapper;
import com.example.wxai.pijo.dto.MyInfoDto;
import com.example.wxai.pijo.entity.Comment;
import com.example.wxai.pijo.entity.Essay;
import com.example.wxai.pijo.entity.Likes;
import com.example.wxai.pijo.entity.User;
import com.example.wxai.pijo.vo.HandUpVo;
import com.example.wxai.pijo.vo.IndexVo;
import com.example.wxai.result.PageResult;
import com.example.wxai.service.IEssayService;
import com.example.wxai.utils.BeanCopyUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-13
 */
@Service
public class EssayServiceImpl extends ServiceImpl<EssayMapper, Essay> implements IEssayService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private LikesMapper likesMapper;

    @Resource
    private CommentMapper commentMapper;
    @Override
    public PageResult<HandUpVo> queryPage(Integer page) {
        //创建分页对象
        Page<Essay> essayList=Page.of(page,10);
        lambdaQuery().page(essayList);
        PageResult<HandUpVo> pageResult=new PageResult<>();
        pageResult.setTotal(essayList.getTotal());
        List<HandUpVo> handUpVoList = BeanCopyUtils.
                copyListProperties(essayList.getRecords(),HandUpVo::new);
        handUpVoList.forEach(e->{
            e.setEssayImgs(e.getEssayImgs().split(",")[0]);
        });
        pageResult.setList(handUpVoList);
        return pageResult;

    }

    @Override
    public PageResult<IndexVo> queryIndexPage(Integer page, String key) {
        //创建分页对象
        Page<Essay> essayList=Page.of(page,10);
           lambdaQuery().like(StringUtils.isBlank(key),Essay::getEssayTitle,key).page(essayList);
        PageResult<IndexVo> pageResult=new PageResult<>();
        pageResult.setTotal(essayList.getTotal());
        List<IndexVo> indexVoList=BeanCopyUtils.copyListProperties(essayList.getRecords(),IndexVo::new);
        for (int i = 0; i < indexVoList.size(); i++) {
            QueryWrapper<User> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("user_id",indexVoList.get(i).getEssayAuthorId());
            User user = userMapper.selectOne(queryWrapper);
            indexVoList.get(i).setEssayAuthorImg(user.getUserImg());

            String[] split = essayList.getRecords().get(i).getEssayImgs().split(",");
            indexVoList.get(i).setEssayImgs(split);
        }
        pageResult.setList(indexVoList);
        return  pageResult;
    }

    @Override
    public PageResult<HandUpVo> queryINfoPage(MyInfoDto myInfoDto) {
        //创建分页对象
        Page<Essay> essayList = Page.of(myInfoDto.getPageNo(), 10);
        PageResult<HandUpVo> pageResult = new PageResult<>();
        List<Likes> likes = null;
        if (myInfoDto.getInfo().equals("我的帖子")) {
            lambdaQuery().eq(Essay::getEssayAuthorId, myInfoDto.getUserId()).page(essayList);
        } else if (myInfoDto.getInfo().equals("我的点赞")) {

            Page<Likes> page = new Page<>(myInfoDto.getPageNo(), 10);  // 第1页，每页10条数据
            QueryWrapper<Likes> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", myInfoDto.getUserId());
            Page<Likes> likesPage = likesMapper.selectPage(page, queryWrapper);
            likes = likesPage.getRecords();
            List<String> list = new ArrayList<>();
            likes.forEach(e -> {
                list.add(e.getEssayId());
            });
            List<Essay> essays = listByIds(list);
            pageResult.setTotal(likesPage.getTotal());
            List<HandUpVo> handUpVoList = BeanCopyUtils.copyListProperties(essays, HandUpVo::new);
            handUpVoList.forEach(e -> {
                e.setEssayImgs(e.getEssayImgs().split(",")[0]);
            });
            pageResult.setList(handUpVoList);
            return pageResult;
        } else {
            Page<Comment> page = new Page<>(myInfoDto.getPageNo(), 10);  // 第1页，每页10条数据
            QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", myInfoDto.getUserId()).select("DISTINCT essay_id");
            Page<Comment> commentPages = commentMapper.selectPage(page, queryWrapper);
            List<Comment> comments = commentPages.getRecords();  // 获取前10条记录
            List<String> list = new ArrayList<>();
            comments.forEach(e -> {
                list.add(e.getEssayId());
            });
            List<Essay> essays = listByIds(list);
            pageResult.setTotal(commentPages.getTotal());
            List<HandUpVo> handUpVoList = BeanCopyUtils.copyListProperties(essays, HandUpVo::new);
            handUpVoList.forEach(e -> {
                e.setEssayImgs(e.getEssayImgs().split(",")[0]);
            });
            pageResult.setList(handUpVoList);
            return pageResult;
        }
        pageResult.setTotal(essayList.getTotal());
        List<HandUpVo> handUpVoList = BeanCopyUtils.copyListProperties(essayList.getRecords(), HandUpVo::new);
        handUpVoList.forEach(e -> {
            e.setEssayImgs(e.getEssayImgs().split(",")[0]);
        });
        pageResult.setList(handUpVoList);
        return pageResult;

    }
}
